{% extends "base.html" %}

{% block title10 %}
    active
{% endblock %}
{% block page-css %}
    <style>
        .hide {
            display: none;
        }
    </style>


{% endblock %}
{% block page-body %}
    <h1 class="page-header">数据添加</h1>

    <!--面板 开始-->
    <div class="panel panel-primary">
    <div class="panel-heading">主机用户密码</div>
    <div class="panel-body">


        <div class="form-inline">
            <div class="form-group">
                <div class="form-group ">
                    <label for="ip">IP</label>
                    <input name="ip" type="text" class="form-control  " id="ip" value="10.">
                    <span class="help-block hide" style="color: red;font-size: 12px;padding-left: 14px" id="msg">IP错误或不存在，请先添加Host列表</span>
                </div>
            </div>
            <div class="form-group form-inline">
                <div class=" form-group ">
                    <label for="user">账户</label>
                    <input name="user" type="text" style="display: inline" class="form-control" id="user" value="aqjg">
                </div>
            </div>
            <div class="form-group form-inline">
                <div class="form-group form-inline ">
                    <label for="password">密码</label>
                    <input name="password" type="text" class="form-control" id="password">
                </div>
            </div>

            <div class="form-group form-inline">
                <label for="rootpwd">root密码</label>
                <input name="rootpwd" type="text" class="form-control" id="rootpwd">
            </div>

            <div class="form-group form-inline"><strong>批量管理</strong>
                <select id="management" name="management" class="form-control">
                    <option value="可以">YES</option>
                    <option value="不可以">NO</option>
                    <option selected value="">未知</option>
                </select>
            </div>
            <button id="add1" type="submit" name="operating" value="1" class="btn btn-success ">添加</button>
            <button id="add2" type="submit" name="operating" value="2" class="btn btn-success ">再次添加</button>

            {#                        </form>#}


        </div>
        <!--面板 结束-->
    </div>
{% endblock %}

{% block page-script %}
    <script src="/static/jquery.cookie.js"></script>
    <script type="text/javascript">

        $(function () {

            var old_ip = $('#ip').val();
            $("#ip").change(function () {
                var new_ip = $('#ip').val();
                if (new_ip !== old_ip) {
                    {#ajax 获取cookie中的csrf_token CSRF添加至请求头部#}
                    var token=$.cookie('csrftoken');
                    $.ajax({
                        headers:{"X-CSRFToken":token},
                        url: "/ip_check/",
                        type: "post",
                        data: {"ip": new_ip},
                        dataType: 'json',
                        success: function (data) {
                            {#console.log(data.status,data.message);#}
                            if (data.status) {
                                $("#msg").text("IP正确");
                                $("#msg").removeClass("hide");
                                setTimeout('$("#msg").addClass("hide");', 2000);

                            } else {
                                $("#msg").text(data.message);
                                $("#msg").removeClass("hide");
                            }
                        }
                    })
                }
            });


            $("#add1").on('click', function () {
                {#ajax 获取cookie中的csrf_token CSRF添加至请求头部#}
                var token=$.cookie('csrftoken');
                $.ajax({
                    headers:{"X-CSRFToken":token},
                    url: "/add_userinfo.html",
                    type: "post",
                    data: {
                        'ip': $("#ip").val(),
                        'user': $("#user").val(),
                        'password': $("#password").val(),
                        'rootpwd': $("#rootpwd").val(),
                        'management': $("#management").val(),
                        'operating': $("#operating").val(),
                    },
                });
                location.href = "/userinfo.html";
            });
            $("#add2").on('click', function () {
                {#ajax 获取cookie中的csrf_token CSRF添加至请求头部#}
                var token=$.cookie('csrftoken');
                $.ajax({
                    headers:{"X-CSRFToken":token},
                    url: "/add_userinfo.html",
                    type: "post",
                    data: {
                        'ip': $("#ip").val(),
                        'user': $("#user").val(),
                        'password': $("#password").val(),
                        'rootpwd': $("#rootpwd").val(),
                        'management': $("#management").val(),
                        'operating': $("#operating").val(),
                    },
                });
                location.reload()
            });

        });


    </script>
{% endblock %}